package phapp.action.custom;

import java.io.*;
import java.util.*;
import javax.servlet.http.*;

import net.sf.json.JSONObject;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import javacommon.jquery.JqueryPageHelper;
import javacommon.jquery.ListRange;
import javacommon.util.StringTool;

import phapp.model.custom.Custom;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;
import static javacommon.util.Struts2JsonHelper.*;

import javacommon.base.*;

import phapp.model.custom.*;
import phapp.dao.custom.*;
import phapp.service.custom.impl.*;
import phapp.service.custom.*;
import phapp.service.luckstar.StaruseManager;
import phapp.service.qfd.CustqfdManager;

/**
 * Title: t_custom_info Description: 接收前面界面请求,通过json数据格式交互 Action Description:
 * 页面通过Ajax调用Action层,返回JSONO数据 Copyright: Copyright (c) 2012-10 Company:
 * 
 * @author you name
 * @version 1.0
 */

public class CustomAction extends BaseStruts2Action
{

   private CustomManager customManager;
   private CustqfdManager custqfdManager;
   private StaruseManager staruseManager;
   //private FeedbackManager feedbackManager;

   /** 通过spring自动注入 */
   public void setCustomManager(CustomManager manager)
   {
      this.customManager = manager;
   }

//   public void setFeedbackManager(FeedbackManager feedbackManager)
//   {
//      this.feedbackManager = feedbackManager;
//   }

   public void setCustqfdManager(CustqfdManager custqfdManager)
   {
      this.custqfdManager = custqfdManager;
   }

   public void setStaruseManager(StaruseManager staruseManager)
   {
      this.staruseManager = staruseManager;
   }

   /**
    * jqGrid使用列表 分页查询
    * 
    * @throws IOException
    * @author
    */
   public void list() throws IOException
   {
      PageRequest<Map> pr = JqueryPageHelper.createPageRequestForJquery(getRequest());
      Page page = customManager.findByPageRequest(pr);

      List<Custom> Customlist = (List) page.getResult();
      ListRange<Custom> resultList = new ListRange<Custom>();
      resultList.setDataRows(Customlist);
      resultList.setRecords(page.getTotalCount());
      resultList.setRows(page.getPageSize());
      if (page.getTotalCount() % page.getPageSize() > 0)
      {
         resultList.setTotal(page.getTotalCount() / page.getPageSize() + 1);
      }
      else
      {
         resultList.setTotal(page.getTotalCount() / page.getPageSize());
      }
      resultList.setPage(pr.getPageNumber());
      outJson(resultList);
   }

   /**
    * Custom添加
    * 
    * @author
    * @throws IOException
    */
   public void save() throws IOException
   {
//      Map<String, Object> result = new HashMap<String, Object>();
//      try
//      {
//         HttpServletRequest request = getRequest();
//         String customJson = request.getParameter("customJson");
//         if (null != customJson && !"".equals(customJson))
//         {
//            JSONObject jsonobject = JSONObject.fromObject(customJson);
//            Custom customobj = (Custom) JSONObject.toBean(jsonobject, Custom.class);
//            customManager.save(customobj);
//            result.put("success", true);
//            result.put("msg", "添加成功!");
//         }
//         else
//         {
//            result.put("success", false);
//            result.put("msg", "添加失败 数据错误!");
//         }
//      }
//      catch (Exception e)
//      {
//         result.put("success", false);
//         result.put("msg", "添加失败!");
//         e.printStackTrace();
//      }
//      outJson(result);
   }

   /**
    * Custom修改
    * 
    * @throws IOException
    * @author
    */
   public void update() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String customJson = request.getParameter("customJson");
         if (null != customJson && !"".equals(customJson))
         {
            JSONObject jsonobject = JSONObject.fromObject(customJson);
            Custom customobj = (Custom) JSONObject.toBean(jsonobject, Custom.class);

            /** 设置主键值 */
            // customobj.setId(jsonobject.get("id").toString());
            customobj.setId(new Integer(jsonobject.get("id").toString()));

            customManager.update(customobj);
            result.put("success", true);
            result.put("msg", "修改成功!");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "修改失败 数据错误!");
         }
      }
      catch (Exception e)
      {
         result.put("success", false);
         result.put("msg", "修改失败!");
         e.printStackTrace();
      }
      
      outJson(result);
   }

   /**
    * Custom删除
    * 
    * @throws IOException
    * @author
    */
   public void delete() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String ids = request.getParameter("ids");
         if (null != ids && !"".equals(ids))
         {
            Custom custom = new Custom();
            custom.setIdList(StringTool.stringToArray(ids));
            customManager.removeByIdArry(custom);
            result.put("success", true);
            result.put("msg", "删除成功");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "删除失败 数据错误!");
         }
      }
      catch (Exception e)
      {
         result.put("success", false);
         result.put("msg", "删除失败!");
         e.printStackTrace();
      }
      outJson(result);
   }

   
   public void regist() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      
      try
      {
         HttpServletRequest request = getRequest();
         String customJson = request.getParameter("customJson");
         if (null != customJson && !"".equals(customJson))
         {
            JSONObject jsonCustom = JSONObject.fromObject(customJson);
            
            result = customManager.customRegist(jsonCustom);
         }
         else
         {
            result.put("statusCode", 10000);
            result.put("message", "注册数据错误！");
         }
      }
      catch (Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "注册异常！");
         e.printStackTrace();
      }
      
      outJson(result);
   }

   public void updateCustom() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      
      try
      {
         HttpServletRequest request = getRequest();
         String customJson = request.getParameter("customJson");
         if (null != customJson && !"".equals(customJson))
         {
            JSONObject jsonCustom = JSONObject.fromObject(customJson);
            
            result = customManager.customUpdate(jsonCustom);
         }
         else
         {
            result.put("statusCode", 10000);
            result.put("message", "参数错误！");
         }
      }
      catch (Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "修改资料异常！");
         e.printStackTrace();
      }
      
      outJson(result);
   }
   
   public void login() throws IOException
   {
      //HttpSession userSession = null;
      Map<String, Object> result = new HashMap<String, Object>();
      
      try
      {
         HttpServletRequest request = getRequest();
         //request.getSession();
         String customJson = request.getParameter("customJson");
         if (null != customJson && !"".equals(customJson))
         {
            JSONObject jsonCustom = JSONObject.fromObject(customJson);
            
            result = customManager.customLogin(jsonCustom);
         }
         else
         {
            result.put("statusCode", 10000);
            result.put("message", "登录数据错误！");
         }
      }
      catch (Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "登录异常！");
         e.printStackTrace();
      }
      
      outJson(result);
   }

   public void forecast()
   {
      Map<String, Object> result = new HashMap<String, Object>();
      
      try
      {
         HttpServletRequest request = getRequest();
         String customJson = request.getParameter("customJson");
         if (null != customJson && !"".equals(customJson))
         {
            JSONObject jsonCustom = JSONObject.fromObject(customJson);
            
            result = customManager.customForecast(jsonCustom);
         }
         else
         {
            result.put("statusCode", 10000);
            result.put("message", "开运数据错误！");
         }
      }
      catch (Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "开运异常！");
         e.printStackTrace();
      }
      
      outJson(result);
   }
   
   public void prayLuck()
   {
      Map<String, Object> result = new HashMap<String, Object>();
      
      try
      {
         HttpServletRequest request = getRequest();
         String customJson = request.getParameter("customJson");
         if (null != customJson && !"".equals(customJson))
         {
            JSONObject jsonCustom = JSONObject.fromObject(customJson);
            
            result = customManager.customPray(jsonCustom);
         }
         else
         {
            result.put("statusCode", 10000);
            result.put("message", "开运数据错误！");
         }
      }
      catch (Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "开运异常！");
         e.printStackTrace();
      }
      
      outJson(result);
   }
   
   public void feedback() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      
      try
      {
         HttpServletRequest request = getRequest();
         String customJson = request.getParameter("feedbackJson");
         if (null != customJson && !"".equals(customJson))
         {
            JSONObject jsonCustom = JSONObject.fromObject(customJson);
            
            //result = feedbackManager.customFeedback(jsonCustom);
         }
         else
         {
            result.put("statusCode", 10000);
            result.put("message", "开运数据错误！");
         }
      }
      catch (Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "开运异常！");
         e.printStackTrace();
      }
      
      outJson(result);
   }
   
   
   
   public void useLight() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      
      try
      {
         HttpServletRequest request = getRequest();
         String customJson = request.getParameter("jsonParam");
         if (null != customJson && !"".equals(customJson))
         {
            JSONObject jsonCustom = JSONObject.fromObject(customJson);
            
            result = custqfdManager.customUseLight(jsonCustom);
         }
         else
         {
            result.put("statusCode", 10000);
            result.put("message", "放灯参数错误！");
         }
      }
      catch (Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "放灯异常！");
         e.printStackTrace();
      }
      
      outJson(result);
   }
   
   public void increase() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      
      try
      {
         HttpServletRequest request = getRequest();
         String customJson = request.getParameter("jsonParam");
         if (null != customJson && !"".equals(customJson))
         {
            JSONObject jsonCustom = JSONObject.fromObject(customJson);
            
            result = custqfdManager.customIncreaseOil(jsonCustom);
         }
         else
         {
            result.put("statusCode", 10000);
            result.put("message", "添加灯油参数错误！");
         }
      }
      catch (Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "添加灯油操作异常！");
         e.printStackTrace();
      }
      
      outJson(result);
   }
   
   public void discuss() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      
      try
      {
         HttpServletRequest request = getRequest();
         String customJson = request.getParameter("feedbackJson");
         if (null != customJson && !"".equals(customJson))
         {
            JSONObject jsonCustom = JSONObject.fromObject(customJson);
            
            //result = feedbackManager.customFeedback(jsonCustom);
         }
         else
         {
            result.put("statusCode", 10000);
            result.put("message", "开运数据错误！");
         }
      }
      catch (Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "开运异常！");
         e.printStackTrace();
      }
      
      outJson(result);
   }
   
   public void getDiscuss() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      
      try
      {
         HttpServletRequest request = getRequest();
         String customJson = request.getParameter("feedbackJson");
         if (null != customJson && !"".equals(customJson))
         {
            JSONObject jsonCustom = JSONObject.fromObject(customJson);
            
            //result = feedbackManager.customFeedback(jsonCustom);
         }
         else
         {
            result.put("statusCode", 10000);
            result.put("message", "开运数据错误！");
         }
      }
      catch (Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "开运异常！");
         e.printStackTrace();
      }
      
      outJson(result);
   }
}
